package com.quanyan.stadiumScheduler.service.impl;

import com.quanyan.messagepush.enums.IdType;
import com.quanyan.messagepush.enums.MsgType;
import com.quanyan.stadiumScheduler.enums.GameTypeConfigEnum;
import com.quanyan.stadiumScheduler.utils.DateUtil;
import com.quanyan.stadiumScheduler.constants.GameConstants;
import com.quanyan.stadiumScheduler.entity.TbGameCommonParameter;
import com.quanyan.stadiumScheduler.entity.TbGameEvent;
import com.quanyan.stadiumScheduler.pushMsg.stadiumMsgPush;
import com.quanyan.stadiumScheduler.pushMsg.StadiumMsgModel;
import com.quanyan.stadiumScheduler.service.BaseService;
import com.quanyan.stadiumScheduler.service.ConfigService;
import com.quanyan.stadiumScheduler.service.GameMsgPush;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;

/**
 * @author lds
 * @date 2017/2/14 16:30
 *
 * 运动场,赛事相关的消息推送
 */

@Service
public class GameMsgPushImpl implements GameMsgPush {

    private final static Logger logger = LoggerFactory.getLogger(GameMsgPushImpl.class);


    private final static int MAX_UID_LIST_SIZE = 50;

    @Autowired
    private BaseService baseService;

    @Autowired
    private stadiumMsgPush stadiumMsgPush;

    @Autowired
    private ConfigService configService;

    @Override
    public void pushGameStartMsgToUser(TbGameEvent tbGameEvent, List<Integer> uids) {
        if (null == uids || uids.size() < 1) {
            return;
        }
        HashSet<Integer> tempSet = new HashSet<>(uids);
        uids = new ArrayList<>(tempSet);
        TbGameCommonParameter tbGameCommonParameter = null;
        if (tbGameEvent.getGameTeamType().intValue() == GameConstants.TEAM_TYPE_CLUB.intValue()) {
            tbGameCommonParameter = baseService.queryTbGameCommonParameter(GameConstants.STADIUM_DEFAULT_GROUP_PARAMETER_KEY,
                    stadiumMsgPush.GAME_OFFICIAL_TEAM_CLUB_START_MSG);
        } else {
            tbGameCommonParameter = baseService.queryTbGameCommonParameter(GameConstants.STADIUM_DEFAULT_GROUP_PARAMETER_KEY,
                    stadiumMsgPush.GAME_OFFICIAL_TEAM_PERSON_START_MSG);
        }
        String body = stadiumMsgPush.msgUtils(tbGameCommonParameter.getParameterValue(), tbGameEvent.getName(),
                tbGameEvent.getManagerName() != null ? tbGameEvent.getManagerName() : configService.getProperty("pk.alert.title", "百灵鸟"));
        int n = uids.size() / MAX_UID_LIST_SIZE + 1;
        for (int i = 0; i < n; i++) {
            StadiumMsgModel stadiumMsgModel = new StadiumMsgModel();
            stadiumMsgModel.setAlertTitle(configService.getProperty("pk.alert.title", "百灵鸟"));
            stadiumMsgModel.setMsgTitle(body);
            stadiumMsgModel.setAlertBody(configService.getProperty("pk.event.push.title", "赛事消息") + ":" + body);
            stadiumMsgModel.setMsgType(MsgType.NOTIFY_MSG);
            stadiumMsgModel.setToUidList(uids.subList(i * MAX_UID_LIST_SIZE, (i + 1) == n ? uids.size() : (i + 1) * MAX_UID_LIST_SIZE));
            stadiumMsgPush.pushMessageOperate(stadiumMsgModel);
        }
        logger.info("赛事开始, 提醒用户参加,当前赛事:{}", tbGameEvent.getId());
    }

    @Override
    public void pushGameFinishPushMsgToUser(TbGameEvent tbGameEvent, List<Integer> uIds) {
        if (uIds == null || uIds.size() < 1) {
            return;
        }
        HashSet<Integer> tempSet = new HashSet<>(uIds);
        uIds = new ArrayList<>(tempSet);
        TbGameCommonParameter tbGameCommonParameter = baseService.queryTbGameCommonParameter(GameConstants.STADIUM_DEFAULT_GROUP_PARAMETER_KEY,
                stadiumMsgPush.GAME_OFFICIAL_EVENT_EDN_MSG);
        String body = DateUtil.getSpecialDateStr() + stadiumMsgPush.msgUtilsReplaceXXX(tbGameCommonParameter.getParameterValue(), tbGameEvent.getName());
        int n = uIds.size() / MAX_UID_LIST_SIZE + 1;
        Map<String, Object> params = new HashMap();
        params.put("gameEventId", (long) tbGameEvent.getId());
        params.put("gameEventType", tbGameEvent.getGameEventType());
        params.put("categoryId", tbGameEvent.getCategoryId());
        for (int i = 0; i < n; i++) {
            StadiumMsgModel stadiumMsgModel = new StadiumMsgModel();
            stadiumMsgModel.setMsgTitle(body);
            stadiumMsgModel.setParams(params);
            stadiumMsgModel.setAlertTitle(configService.getProperty("pk.alert.title", "百灵鸟"));
            stadiumMsgModel.setAlertBody(configService.getProperty("pk.event.push.title", "赛事消息") + ":" + body);
            stadiumMsgModel.setMsgType(MsgType.JUMP_MSG);
            //由于业务调整，赛事类型增加，跳转链接由 方法getGameDetailByType 提供
//            if(tbGameEvent.getGameSystemType() == GameSystemConfigEnum.CONFIG_GAME_TYPE_PK.getId()) {
//                stadiumMsgModel.setIdType(IdType.STADIUM_GAME_PK_DETAIL);
//            }else{
//                stadiumMsgModel.setIdType(IdType.STADIUM_GAME_EVENT_DETAIL);
//            }
            stadiumMsgModel.setIdType(this.getGameDetailByType(tbGameEvent.getGameEventType()));

            stadiumMsgModel.setToUidList(uIds.subList(i * MAX_UID_LIST_SIZE, (i + 1) == n ? uIds.size() : (i + 1) * MAX_UID_LIST_SIZE));
            stadiumMsgPush.pushMessageOperate(stadiumMsgModel);
        }
        logger.info("官方赛事完结,记录用户的赛事数目以及赛程数目,当前赛事:{}", tbGameEvent.getId());
    }

    @Override
    public void unNormalEnd(TbGameEvent tbGameEvent, List<Integer> uids) {
        if (uids == null || uids.size() == 0) {
            return;
        }
        TbGameCommonParameter tbGameCommonParameter = baseService.queryTbGameCommonParameter(GameConstants.STADIUM_DEFAULT_GROUP_PARAMETER_KEY,
                stadiumMsgPush.GAME__UNNORMAL_END);
        String body = DateUtil.getSpecialDateStr() + stadiumMsgPush.msgUtilsReplaceFirstXXX(tbGameCommonParameter.getParameterValue(), tbGameEvent.getName());
        StadiumMsgModel stadiumMsgModel = new StadiumMsgModel();
        Map<String, Object> params = new HashMap();
        params.put("gameEventId", (long) tbGameEvent.getId());
        params.put("gameEventType", tbGameEvent.getGameEventType());
        params.put("categoryId", tbGameEvent.getCategoryId());
        stadiumMsgModel.setParams(params);
        stadiumMsgModel.setMsgTitle(body);
        stadiumMsgModel.setAlertTitle(configService.getProperty("pk.alert.title", "百灵鸟"));
        stadiumMsgModel.setAlertBody(configService.getProperty("pk.event.push.title", "赛事消息") + ":" + body);
        stadiumMsgModel.setMsgType(MsgType.JUMP_MSG);
        stadiumMsgModel.setIdType(IdType.STADIUM_GAME_PK_DETAIL);
        stadiumMsgModel.setToUidList(uids);
        stadiumMsgPush.pushMessageOperate(stadiumMsgModel);
    }

    @Override
    public void pushBeforeOneHourGameStartMsgToUser(TbGameEvent tbGameEvent, List<Integer> uids) {
        if (uids == null || uids.size() < 1) {
            return;
        }
        HashSet<Integer> tempSet = new HashSet<>(uids);
        uids = new ArrayList<>(tempSet);
        TbGameCommonParameter tbGameCommonParameter = null;
        if (tbGameEvent.getGameTeamType().intValue() == GameConstants.TEAM_TYPE_CLUB.intValue()) {
            tbGameCommonParameter = baseService.queryTbGameCommonParameter(GameConstants.STADIUM_DEFAULT_GROUP_PARAMETER_KEY,
                   stadiumMsgPush.GAME_OFFICIAL_TEAM_CLUB_SCHEDULE_ONE_HOUR_START_MSG);
        } else {
            tbGameCommonParameter = baseService.queryTbGameCommonParameter(GameConstants.STADIUM_DEFAULT_GROUP_PARAMETER_KEY,
                    stadiumMsgPush.GAME_OFFICIAL_TEAM_PERSON_SCHEDULE_ONE_HOUR_START_MSG);
        }
        Map<String, Object> params = new HashMap();
        params.put("gameEventId", (long) tbGameEvent.getId());
        params.put("gameEventType", tbGameEvent.getGameEventType());
        params.put("categoryId", tbGameEvent.getCategoryId());
        String body = DateUtil.getSpecialDateStr() + stadiumMsgPush.msgUtilsReplaceXXX(tbGameCommonParameter.getParameterValue(), tbGameEvent.getName());
        int n = uids.size() / MAX_UID_LIST_SIZE + 1;
        for (int i = 0; i < n; i++) {
            StadiumMsgModel stadiumMsgModel = new StadiumMsgModel();
            stadiumMsgModel.setMsgTitle(body);
            stadiumMsgModel.setParams(params);
            stadiumMsgModel.setAlertTitle(configService.getProperty("pk.alert.title", "百灵鸟"));
            stadiumMsgModel.setAlertBody(configService.getProperty("pk.event.push.title", "赛事消息") + ":" + body);
            stadiumMsgModel.setMsgType(MsgType.JUMP_MSG);
            //PK赛
            if(tbGameEvent.getGameSystemType() == 6) {
                stadiumMsgModel.setIdType(IdType.STADIUM_GAME_PK_DETAIL);
            }else{
                stadiumMsgModel.setIdType(IdType.STADIUM_GAME_EVENT_DETAIL);
            }

            stadiumMsgModel.setToUidList(uids.subList(i * MAX_UID_LIST_SIZE, (i + 1) == n ? uids.size() : (i + 1) * MAX_UID_LIST_SIZE));
            stadiumMsgPush.pushMessageOperate(stadiumMsgModel);
        }
        logger.info("赛事开始前1小时, 提醒用户参加,当前赛事:{}", tbGameEvent.getId());
    }

    /**
     * 根据赛事类型跳转详情
     *
     *
     * */
    private IdType getGameDetailByType(int gameType){
        IdType result = null;
        if(gameType == GameTypeConfigEnum.CONFIG_GAME_EVENT_TYPE_PERSONAL.getId()) {
            result = IdType.STADIUM_GAME_PK_DETAIL;
        }else if (gameType  == GameTypeConfigEnum.CONFIG_GAME_EVENT_TYPE_SCENE.getId()){
            result = IdType.STADIUM_GAME_SCENE_DETAIL;
        }else if (gameType  == GameTypeConfigEnum.CONFIG_GAME_EVENT_TYPE_INTERNAL.getId()){
            result = IdType.STADIUM_GAME_INTERNAL_DETAIL;
        }else {
            result = IdType.STADIUM_GAME_EVENT_DETAIL;
        }
        return result;
    }

}
